home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000050.txt < prev    next >
Text File  |  2013-04-03  |  6KB  |  160 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the webRequest API.
  6.  
  7. var webRequestNatives = requireNative('web_request');
  8. var GetUniqueSubEventName = webRequestNatives.GetUniqueSubEventName;
  9.  
  10. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  11. var sendRequest = require('sendRequest').sendRequest;
  12. var validate = require('schemaUtils').validate;
  13.  
  14. // WebRequestEvent object. This is used for special webRequest events with
  15. // extra parameters. Each invocation of addListener creates a new named
  16. // sub-event. That sub-event is associated with the extra parameters in the
  17. // browser process, so that only it is dispatched when the main event occurs
  18. // matching the extra parameters.
  19. //
  20. // Example:
  21. //   chrome.webRequest.onBeforeRequest.addListener(
  22. //       callback, {urls: 'http://*.google.com/*'});
  23. //   ^ callback will only be called for onBeforeRequests matching the filter.
  24. function WebRequestEvent(eventName, opt_argSchemas, opt_extraArgSchemas,
  25.                          opt_eventOptions) {
  26.   if (typeof eventName != 'string')
  27.     throw new Error('chrome.WebRequestEvent requires an event name.');
  28.  
  29.   this.eventName_ = eventName;
  30.   this.argSchemas_ = opt_argSchemas;
  31.   this.extraArgSchemas_ = opt_extraArgSchemas;
  32.   this.subEvents_ = [];
  33.   this.eventOptions_ = chromeHidden.parseEventOptions(opt_eventOptions);
  34.   if (this.eventOptions_.supportsRules) {
  35.     this.eventForRules_ =
  36.         new chrome.Event(eventName, opt_argSchemas, opt_eventOptions);
  37.   }
  38. };
  39.  
  40. // Test if the given callback is registered for this event.
  41. WebRequestEvent.prototype.hasListener = function(cb) {
  42.   if (!this.eventOptions_.supportsListeners)
  43.     throw new Error('This event does not support listeners.');
  44.   return this.findListener_(cb) > -1;
  45. };
  46.  
  47. // Test if any callbacks are registered fur thus event.
  48. WebRequestEvent.prototype.hasListeners = function() {
  49.   if (!this.eventOptions_.supportsListeners)
  50.     throw new Error('This event does not support listeners.');
  51.   return this.subEvents_.length > 0;
  52. };
  53.  
  54. // Registers a callback to be called when this event is dispatched. If
  55. // opt_filter is specified, then the callback is only called for events that
  56. // match the given filters. If opt_extraInfo is specified, the given optional
  57. // info is sent to the callback.
  58. WebRequestEvent.prototype.addListener =
  59.     function(cb, opt_filter, opt_extraInfo) {
  60.   if (!this.eventOptions_.supportsListeners)
  61.     throw new Error('This event does not support listeners.');
  62.   var subEventName = GetUniqueSubEventName(this.eventName_);
  63.   // Note: this could fail to validate, in which case we would not add the
  64.   // subEvent listener.
  65.   validate(Array.prototype.slice.call(arguments, 1), this.extraArgSchemas_);
  66.   chromeHidden.internalAPIs.webRequestInternal.addEventListener(
  67.       cb, opt_filter, opt_extraInfo, this.eventName_, subEventName);
  68.  
  69.   var subEvent = new chrome.Event(subEventName, this.argSchemas_);
  70.   var subEventCallback = cb;
  71.   if (opt_extraInfo && opt_extraInfo.indexOf('blocking') >= 0) {
  72.     var eventName = this.eventName_;
  73.     subEventCallback = function() {
  74.       var requestId = arguments[0].requestId;
  75.       try {
  76.         var result = cb.apply(null, arguments);
  77.         chromeHidden.internalAPIs.webRequestInternal.eventHandled(
  78.             eventName, subEventName, requestId, result);
  79.       } catch (e) {
  80.         chromeHidden.internalAPIs.webRequestInternal.eventHandled(
  81.             eventName, subEventName, requestId);
  82.         throw e;
  83.       }
  84.     };
  85.   } else if (opt_extraInfo && opt_extraInfo.indexOf('asyncBlocking') >= 0) {
  86.     var eventName = this.eventName_;
  87.     subEventCallback = function() {
  88.       var details = arguments[0];
  89.       var requestId = details.requestId;
  90.       var handledCallback = function(response) {
  91.         chromeHidden.internalAPIs.webRequestInternal.eventHandled(
  92.             eventName, subEventName, requestId, response);
  93.       };
  94.       cb.apply(null, [details, handledCallback]);
  95.     };
  96.   }
  97.   this.subEvents_.push(
  98.       {subEvent: subEvent, callback: cb, subEventCallback: subEventCallback});
  99.   subEvent.addListener(subEventCallback);
  100. };
  101.  
  102. // Unregisters a callback.
  103. WebRequestEvent.prototype.removeListener = function(cb) {
  104.   if (!this.eventOptions_.supportsListeners)
  105.     throw new Error('This event does not support listeners.');
  106.   var idx;
  107.   while ((idx = this.findListener_(cb)) >= 0) {
  108.     var e = this.subEvents_[idx];
  109.     e.subEvent.removeListener(e.subEventCallback);
  110.     if (e.subEvent.hasListeners()) {
  111.       console.error(
  112.           'Internal error: webRequest subEvent has orphaned listeners.');
  113.     }
  114.     this.subEvents_.splice(idx, 1);
  115.   }
  116. };
  117.  
  118. WebRequestEvent.prototype.findListener_ = function(cb) {
  119.   for (var i in this.subEvents_) {
  120.     var e = this.subEvents_[i];
  121.     if (e.callback === cb) {
  122.       if (e.subEvent.findListener_(e.subEventCallback) > -1)
  123.         return i;
  124.       console.error('Internal error: webRequest subEvent has no callback.');
  125.     }
  126.   }
  127.  
  128.   return -1;
  129. };
  130.  
  131. WebRequestEvent.prototype.addRules = function(rules, opt_cb) {
  132.   if (!this.eventOptions_.supportsRules)
  133.     throw new Error('This event does not support rules.');
  134.   this.eventForRules_.addRules(rules, opt_cb);
  135. }
  136.  
  137. WebRequestEvent.prototype.removeRules = function(ruleIdentifiers, opt_cb) {
  138.   if (!this.eventOptions_.supportsRules)
  139.     throw new Error('This event does not support rules.');
  140.   this.eventForRules_.removeRules(ruleIdentifiers, opt_cb);
  141. }
  142.  
  143. WebRequestEvent.prototype.getRules = function(ruleIdentifiers, cb) {
  144.   if (!this.eventOptions_.supportsRules)
  145.     throw new Error('This event does not support rules.');
  146.   this.eventForRules_.getRules(ruleIdentifiers, cb);
  147. }
  148.  
  149. chromeHidden.registerCustomEvent('webRequest', WebRequestEvent);
  150.  
  151. chromeHidden.registerCustomHook('webRequest', function(api) {
  152.   var apiFunctions = api.apiFunctions;
  153.  
  154.   apiFunctions.setHandleRequest('handlerBehaviorChanged', function() {
  155.     var args = Array.prototype.slice.call(arguments);
  156.     sendRequest(this.name, args, this.definition.parameters,
  157.                 {forIOThread: true});
  158.   });
  159. });
  160.